package com.hxl.demo.service.Impl;

import com.hxl.demo.entity.Staff;
import com.hxl.demo.entity.User;
import com.hxl.demo.mapper.StaffMapper;
import com.hxl.demo.mapper.UserMapper;
import com.hxl.demo.service.StaffService;
import com.hxl.demo.service.UserService;
import com.hxl.demo.tools.Common;
import com.hxl.demo.tools.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private  UserMapper userMapper;


    @Override
    public Integer getStaffNum(String userName,String pinName,String deptName,String accountType,String tel) {

        return staffMapper.getStaffNum(userName,pinName,deptName,accountType,tel);
    }

    @Override
    public Object staffAdd(Staff staff) {
        staff.setInsertTime((new Date()));

        if(staff.getTel()==null || staff.getTel().equals("")){
            return Common.returnJsonFail("电话号码不能为空",400);
        }
        List<Staff> userList=staffMapper.getStaffList(null,null,"","","","",staff.getTel(),null);

        if(userList!=null && userList.size()>0){
            return Common.returnJsonFail("操作失败，电话号码已存在",400);
        }
        Integer id= staffMapper.staffAdd(staff);
        String password="";
        if(staff.getPassword()!=null &&  staff.getPassword().length()<6 &&  staff.getPassword().length()>20){
            Result.fail().message("密码长度在6-20个字符之间");
        }else if(staff.getPassword()==null){
            password=staff.getTel();
        }else{
            password=staff.getPassword();
        }
        password=DigestUtils.md5DigestAsHex(password.getBytes());

        if(id>0){
            User user=new User();
            user.setUsername(staff.getTel());
            user.setPassword(password);
            user.setStaffId(staff.getId());
            user.setInsertTime(new Date());
            userMapper.userAdd(user);
            return   Result.ok();
        }else{
            return   Result.fail();
        }
    }

    @Override
    public List<Staff> getStaffList(Integer limit,Integer page,String userName,String pinName,String deptName,String accountType,String tel,Integer id) {
        return staffMapper.getStaffList( limit, page, userName, pinName, deptName, accountType, tel,id);
    }

    @Override
    public  Object staffUpdate(Staff staff){
        Integer res= staffMapper.staffUpdate(staff);
        if(res>0){
            return Result.ok();
        }
        return Result.fail();
}

    @Override
    public  Object staffDel(Integer id){
        List<Staff> list=staffMapper.getStaffList(null,null,"","","","","",id);
        if(list!=null && list.size()>0 ){
            userMapper.userDel(list.get(0).getUserId());
        }
         Integer res= staffMapper.staffDel(id);
        if(res>0){
            return Result.ok();
        }
        return Result.fail();
    }
}

